package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.service.ExprService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private ExprService exprService;
    @Autowired
    private DeptService deptService;
    @Autowired
        private DeptMapper deptMapper;
    @Override
    public PageResult<Emp> getPage(EmpQueryParam param) {
        // 创建分页对象
        Page<Emp> empPage = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Emp> empLambdaQueryWrapper = new LambdaQueryWrapper<Emp>()
                .like(!StringUtils.isEmpty(param.getName()),Emp::getName ,param.getName())
                .eq(param.getGender() != null ,Emp::getGender , param.getGender())
                .between(param.getBegin() != null && param.getEnd() !=null,
                        Emp::getEntryDate,
                        param.getBegin(),
                        param.getEnd());

        // 调用 baseMapper 的 selectPage 方法进行分页查询
        this.page(empPage,empLambdaQueryWrapper);
        List<Emp> records = empPage.getRecords();
        records.forEach(emp -> {
            Integer deptId = emp.getDeptId();
            Dept dept = deptMapper.selectById(deptId);
            if (deptId != null){
                emp.setDeptName(dept.getName());
            }



        });

        // 返回分页结果
        return new PageResult<>(empPage.getTotal(), empPage.getRecords());
    }

    @Override
    public void EmpAndExpr(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(empExpr -> {
            empExpr.setEmpId(emp.getId());
        }

        );
        exprService.saveBatch(exprList);
    }

    @Override
    public void listdel(List<Integer> ids) {
        removeBatchByIds(ids);
        exprService.remove(
                new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId,ids)
        );
       


    }

    @Override
    public Emp getId(Integer id) {

        Emp emp = getById(id);
        // 部门名称
        emp.setDeptName(deptService.getById(emp.getDeptId()).getName());
        // 工作经历列表
        emp.setExprList(exprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;

    }

    @Override
    public void upEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        exprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(empExpr -> {
            empExpr.setEmpId(emp.getId());

        });
        exprService.saveBatch(exprList);
    }


}